package com.best.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * https://jingyan.baidu.com/article/d45ad148daf01129542b803e.html
 *
 * @author Jiang Akang
 * employeeId: BG435424
 * @date 2021/10/21
 **/
public class HttpUtils {

    public static String doGet(String url) throws IOException {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建http Get请求
        HttpGet httpGet = new HttpGet(url);
        // response对象
        CloseableHttpResponse response = null;
        try {
            // 执行http get请求
            response = httpClient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println("内容: " + content);
                return content;

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (response != null) {
                response.close();
            }
            httpClient.close();
        }
        return null;
    }

    public static String doPost(String url, Map maps) throws IOException {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建http Post请求
        HttpPost request = new HttpPost();
        CloseableHttpResponse response = null;
        // 设置参数
        List<NameValuePair> nvpl = new ArrayList<>();
        for (Iterator iter = maps.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String value = String.valueOf(maps.get(name));
            nvpl.add(new BasicNameValuePair(name, value));
        }
        try {
            request.setURI(new URI(url));
            request.setEntity(new UrlEncodedFormEntity(nvpl, HTTP.UTF_8));
            // 执行请求
            response = httpClient.execute(request);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println("内容: " + content);
                return content;
            }
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                response.close();
            }
            httpClient.close();
        }
        return null;
    }

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "jak");
        jsonObject.put("age", 26);
        Map map = jsonObject;
        String res = null;
        String reqUrl = "";
        try {
            res = HttpUtils.doPost(reqUrl, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(res);
    }
}
